package org.usts.trainning.supermarket.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.usts.trainning.supermarket.service.UserCommand;

import java.io.IOException;

@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, java.io.IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String next = "index.jsp";
		// 代码（1）通过 if 来实现对不同请求的分发
		if (request.getParameter("command").equals("userList")) {
			UserCommand command = new UserCommand();
			next = command.findAllUser(request, response);
		} else if (request.getParameter("command").equals("lookuser")) {
			UserCommand command = new UserCommand();
			next = command.findUser(request, response);
		} else if (request.getParameter("command").equals("updateuser")) {
			UserCommand command = new UserCommand();
			next = command.updateUser(request, response);
		} else if (request.getParameter("command").equals("modifyuser")) {
			UserCommand command = new UserCommand();
			next = command.updateUserById(request, response);
		} else if (request.getParameter("command").equals("deleteuser")) {
			UserCommand command = new UserCommand();
			next = command.deleteUserById(request, response);
		} else if (request.getParameter("command").equals("login")) {
			UserCommand command = new UserCommand();
			next = command.findUserByName(request, response);
		} else if (request.getParameter("command").equals("adduser")) {
			UserCommand command = new UserCommand();
			next = command.addUser(request, response);
		} else if (request.getParameter("command").equals("modifypwd")) {
			UserCommand command = new UserCommand();
			next = command.modifyPwd(request, response);
		} else if (request.getParameter("command").equals("exit")) {
			UserCommand command = new UserCommand();
			next = command.exit(request, response);
		} else if (request.getParameter("command").equals("searchuser")) {
			UserCommand command = new UserCommand();
			next = command.searchUser(request, response);
		}
		// 代码（2）请求结果转发的视图
		dispatch(request, response, next);
	}

	protected void dispatch(HttpServletRequest request, HttpServletResponse response, String page)
			throws javax.servlet.ServletException, java.io.IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
		dispatcher.forward(request, response);
	}
}
